<?php
declare(strict_types=1);

namespace app\controller\merchant;

use app\BaseController;
use app\model\PlatformOperationLog;
use think\facade\Log;
use think\Request;

class LogController extends BaseController
{
    /**
     * 获取操作日志列表
     */
    public function list(Request $request)
    {
        try {
            $params = $request->get();
            $merchant_id = $request->merchant_id;
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            
            $where = [['merchant_id', '=', $merchant_id]];
            
            // 操作类型筛选
            if (!empty($params['operation_type'])) {
                $where[] = ['operation_type', '=', $params['operation_type']];
            }
            
            // 时间范围筛选
            if (!empty($params['start_time'])) {
                $where[] = ['create_time', '>=', $params['start_time']];
            }
            if (!empty($params['end_time'])) {
                $where[] = ['create_time', '<=', $params['end_time']];
            }
            
            // 关键词搜索
            if (!empty($params['keyword'])) {
                $where[] = ['operation_content', 'like', "%{$params['keyword']}%"];
            }
            
            $list = PlatformOperationLog::where($where)
                ->order('id', 'desc')
                ->paginate([
                    'list_rows' => $limit,
                    'page' => $page
                ]);
            
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
        } catch (\Exception $e) {
            Log::error('获取操作日志列表失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 获取操作类型列表
     */
    public function operationTypes()
    {
        try {
            $types = [
                ['value' => '新增', 'label' => '新增'],
                ['value' => '修改', 'label' => '修改'],
                ['value' => '删除', 'label' => '删除'],
                ['value' => '拉黑用户', 'label' => '拉黑用户'],
                ['value' => '取消拉黑用户', 'label' => '取消拉黑用户'],
                ['value' => '系统自动拉黑用户', 'label' => '系统自动拉黑用户'],
                ['value' => '订单退款', 'label' => '订单退款'],
                ['value' => '订单取消', 'label' => '订单取消'],
                ['value' => '发放优惠券', 'label' => '发放优惠券'],
                ['value' => '配置修改', 'label' => '配置修改']
            ];
            
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $types]);
        } catch (\Exception $e) {
            Log::error('获取操作类型列表失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 导出操作日志
     */
    public function export(Request $request)
    {
        try {
            $params = $request->get();
            $merchant_id = $request->merchant_id;
            
            $where = [['merchant_id', '=', $merchant_id]];
            
            // 操作类型筛选
            if (!empty($params['operation_type'])) {
                $where[] = ['operation_type', '=', $params['operation_type']];
            }
            
            // 时间范围筛选
            if (!empty($params['start_time'])) {
                $where[] = ['create_time', '>=', $params['start_time']];
            }
            if (!empty($params['end_time'])) {
                $where[] = ['create_time', '<=', $params['end_time']];
            }
            
            // 关键词搜索
            if (!empty($params['keyword'])) {
                $where[] = ['operation_content', 'like', "%{$params['keyword']}%"];
            }
            
            $list = PlatformOperationLog::where($where)
                ->field([
                    'operation_type',
                    'operation_content',
                    'create_time'
                ])
                ->order('id', 'desc')
                ->select()
                ->toArray();
            
            // 生成CSV文件
            $filename = 'operation_log_' . date('YmdHis') . '.csv';
            $file = fopen($filename, 'w');
            
            // 写入表头
            fputcsv($file, ['操作类型', '操作内容', '操作时间']);
            
            // 写入数据
            foreach ($list as $item) {
                fputcsv($file, [
                    $item['operation_type'],
                    $item['operation_content'],
                    $item['create_time']
                ]);
            }
            
            fclose($file);
            
            // 下载文件
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . $filename);
            header('Content-Length: ' . filesize($filename));
            readfile($filename);
            unlink($filename);
            
            exit;
        } catch (\Exception $e) {
            Log::error('导出操作日志失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }
} 